package com.example.clock;

import static com.example.clock.MainActivity.DB;
import static com.example.clock.MainActivity.offset;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class ListViewTest extends AppCompatActivity {
    RecyclerView timelist;
    protected class item{
        int id;
        String time;
        TextView shanchu;
        item(int id,String time,TextView shanchu){
            this.id=id;
            this.time=time;
            this.shanchu=shanchu;
        }
    }
    List<item> TList=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view_test);
        //Cursor cursor=DB.rawQuery("select _id,time from list",null);
        //想通过query把数据库DB中存储的数据
        getTimeRecords();
    }

    private void getTimeRecords() {
        try{
            if(!TList.isEmpty()) TList.clear();
            Cursor cursor = DB.query("list", null, null, null, null, null, null);
            if (cursor.moveToFirst()) {
                do {
                    @SuppressLint("Range") int id = cursor.getInt(cursor.getColumnIndex("id"));
                    @SuppressLint("Range") String time = cursor.getString(cursor.getColumnIndex("time"));
                    TList.add(new item(id, time,findViewById(R.id.shanchu)));
                } while (cursor.moveToNext());
            }
            timelist = (RecyclerView) findViewById(R.id.TimeList);
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
            timelist.setLayoutManager(linearLayoutManager);
            timelist.setAdapter(new MyAapter(TList));
            cursor.close();
        }catch (Exception e){
            e.printStackTrace();
            Log.v("cursor","!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
        }
    }

    //自己写的适用于item的适配器
    class MyAapter extends RecyclerView.Adapter<MyAapter.MyViewHolder> {
        public MyAapter(List<item> TList) {
            this.TList = TList;
        }

        private List<item> TList;
        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item,parent,false);
            MyViewHolder myViewHolder=new MyViewHolder(view);
            myViewHolder.clickView.setOnClickListener(view1 -> {
                int i=TList.get(myViewHolder.getAdapterPosition()).id;
                //Toast.makeText(ListViewTest.this,""+i,Toast.LENGTH_SHORT).show();
                String[] num={String.valueOf(i)};
                DB.delete("list","id=?",num);
                DB.execSQL("update list set id=id-1 where id>"+i);
                offset++;
                getTimeRecords();
                //finish();
               // Intent intent=new Intent(ListViewTest.this,ListViewTest.class);
                //startActivity(intent);
            });
            return myViewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
            item item=TList.get(position);
            holder.id.setText(String.valueOf(item.id));
            holder.time.setText(item.time);
        }

        @Override
        public int getItemCount() {
            return TList.size();
        }

        class MyViewHolder extends RecyclerView.ViewHolder {
            TextView id;
            TextView time;
            View clickView;

            public MyViewHolder(View view){
                super(view);
                clickView=view;
                this.id=view.findViewById(R.id.id);
                this.time=view.findViewById(R.id.time);
            }

        }

    }

    @Override
    public void finish() {
        super.finish();
    }
}